<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学生报修</el-breadcrumb-item>
      <el-breadcrumb-item>报修登记</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-form
        :hide-required-asterisk="true"
        :rules="ReportFormRules"
        ref="reportFormRefs"
        :model="reportForm"
        label-width="1.65rem"
      >
        <el-form-item label="报修人">
          <el-input :disabled="true" v-model="reportForm.userNumber"></el-input>
        </el-form-item>

        <el-form-item prop="building" label="楼栋">
          <el-select
            v-model="reportForm.building"
            placeholder="请选择楼栋"
            style="width: 100%"
          >
            <el-option label="北一" value="北一"></el-option>
            <el-option label="北二" value="北二"></el-option>
            <el-option label="北三" value="北三"></el-option>
            <el-option label="北四" value="北四"></el-option>
            <el-option label="信息楼" value="信息楼"></el-option>
            <el-option label="文理楼" value="文理楼"></el-option>
            <el-option label="航海楼" value="航海楼"></el-option>
            <el-option label="轮机楼" value="轮机楼"></el-option>
            <el-option label="图书馆" value="图书馆"></el-option>
            <el-option label="南一" value="南一"></el-option>
            <el-option label="南二" value="南二"></el-option>
            <el-option label="南三" value="南三"></el-option>
            <el-option label="南四" value="南四"></el-option>
            <el-option label="南五" value="南五"></el-option>
            <el-option label="南六" value="南六"></el-option>
            <el-option label="南七" value="南七"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="number" label="宿舍号">
          <el-input v-model="reportForm.number"></el-input>
        </el-form-item>

        <el-form-item prop="repairType" label="维修类型">
          <el-select
            v-model="reportForm.repairType"
            placeholder="请选择维修类型"
            style="width: 100%"
          >
            <el-option label="电类故障" value="电类故障"></el-option>
            <el-option label="水类故障" value="水类故障"></el-option>
            <el-option label="木工类故障" value="木工类故障"></el-option>
            <el-option label="电梯故障" value="电梯故障"></el-option>
            <el-option label="空调故障" value="空调故障"></el-option>
            <el-option label="洗衣机故障" value="洗衣机故障"></el-option>
            <el-option label="直饮水故障" value="直饮水故障"></el-option>
            <el-option label="热水故障" value="热水故障"></el-option>
            <el-option label="其他类故障" value="其他类故障"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="reportDesc" label="故障描述">
          <el-input
            maxlength="100"
            show-word-limit
            type="textarea"
            v-model="reportForm.reportDesc"
          ></el-input>
        </el-form-item>

        <el-form-item label="图片描述">
          <!-- :on-remove="handleRemove" -->
          <el-upload
            class="upload-demo"
            action="http://localhost:8088/uploadPicture"
            :on-success="handleSuccess"
            list-type="picture"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb,只能上传一张
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即登记</el-button>
          <el-button type="info" @click="resetReportForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      reportForm: {},

      ReportFormRules: {
        building: [
          { required: true, message: "请选择楼栋", trigger: "change" },
        ],
        number: [
          { required: true, message: "请输入宿舍号", trigger: "blur" },
          { min: 3, max: 4, message: "长度在3到4个字符", trigger: "blur" },
        ],
        repairType: [
          { required: true, message: "请选择维修类型", trigger: "change" },
        ],
        reportDesc: [
          { required: true, message: "请输入报修描述", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "长度在 1到100 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //表单提交
    onSubmit() {
      this.$refs.reportFormRefs.validate(async (valid) => {
        if (!valid) return false;
        const { data: res } = await this.$http.post(
          "repairbill/report",
          this.reportForm
        );
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);

        this.$router.go(0);

        this.$message.success("报修登记成功 !");
      });
    },

    //文件上传成功返回路径
    handleSuccess(response) {
      alert(response);
      this.$message.success("上传成功");
      this.reportForm.reportImage = response;
    },
    // 重置
    resetReportForm() {
      this.$refs.reportFormRefs.resetFields();
    },
  },
  created() {
    this.reportForm.userNumber = JSON.parse(
      sessionStorage.getItem("user")
    ).username;
  },
};
</script>

<style scoped></style>
